package com.oa.flowable.controller;

import com.oa.common.core.domain.AjaxResult;
import com.oa.flowable.domain.vo.FlowTaskVo;
import com.oa.flowable.service.IFlowInstanceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 工作流流程实例管理
 *
 * @author li.ziye
 * @date 2022-05-14
 */
@Slf4j
@RestController
@RequestMapping("/flowable/instance")
public class FlowInstanceController {

    @Autowired
    private IFlowInstanceService flowInstanceService;

    /**
     * 根据流程定义id启动流程实例
     * @param procDefId 流程id
     * @param variables 变量集合-json
     * @return
     */
    @PostMapping("/startBy/{procDefId}")
    public AjaxResult startById(@PathVariable(value = "procDefId") String procDefId,
                                @RequestBody Map<String, Object> variables) {
        return flowInstanceService.startProcessInstanceById(procDefId, variables);

    }


    /**
     * 激活或挂起流程定义
     * @param state 1:激活,2:挂起
     * @param instanceId 流程实例ID
     * @return
     */
    @PostMapping(value = "/updateState")
    public AjaxResult updateState(@RequestParam Integer state, @RequestParam String instanceId) {
        flowInstanceService.updateState(state,instanceId);
        return AjaxResult.success();
    }

    /**
     * 结束流程实例
     */
    @PostMapping(value = "/stopProcessInstance")
    public AjaxResult stopProcessInstance(@RequestBody FlowTaskVo flowTaskVo) {
        flowInstanceService.stopProcessInstance(flowTaskVo);
        return AjaxResult.success();
    }

    /**
     * 删除流程
     * @param instanceId 流程实例id
     * @param deleteReason 删除原因
     * @return
     */
    @DeleteMapping(value = "/delete")
    public AjaxResult delete(@RequestParam String instanceId,
                             @RequestParam(required = false) String deleteReason) {
        flowInstanceService.delete(instanceId,deleteReason);
        return AjaxResult.success();
    }
}